﻿using System;
using UnityEngine;
using Bingo;
using Bingo.Facades;


public class NumberManager: MonoBehaviour
{
    private int currentToGive = 1;
    private int nextToGive = 1;
    private int MIN_VALUE = 1;
    private int MAX_VALUE = 3;
    private int callCount;
    public int _maxValueInGame = 5; //默认大小5
    public static NumberManager Instance;

    public int currentLevel;
    private int increaseNum = 3;
    private int baseMergeCount = 30;
    private int targetMergeCount;


    private void Awake()
    {
        Instance = this;
        UpgradeRule();
    }

    private void Update()
    {
        Upgrade();
    }

    public void reset()
    {
        this.MIN_VALUE = 1;
        this.MAX_VALUE = 3;
        _maxValueInGame = 5;
        this.callCount = 0;
        this.currentToGive = GetNumber();//UnityEngine.Random.Range(this.MIN_VALUE, this.MAX_VALUE);
        this.nextToGive = GetNumber();// UnityEngine.Random.Range(this.MIN_VALUE, this.MAX_VALUE);
        this.ResetLevel();
    }

    public void SetMaxValueInGame(int number)
    {
        if (this._maxValueInGame < number)
        {
            this._maxValueInGame = number;
            if (_maxValueInGame >= 8)
            {
                //TODO 升级，弹出升级页面
                //GameEntry.BoUI.CreateUI<UIUpgradeComponent>(UIType.UIUpgrade);
                //GameManager.Instance.canFire = false;
            }
        }
        this.MAX_VALUE = Math.Max(this._maxValueInGame - 4, 3);
    }

    public void Upgrade()
    {
        int mergeCount = LocalDB.Get<int>(DBKeys.MergeCount);
        if (mergeCount == targetMergeCount)
        {
//            GameEntry.BoUI.CreateUI<UIUpgradeComponent>(UIType.UIUpgrade);
            GameManager.Instance.canFire = false;
            LocalDB.Save(DBKeys.MergeCount, 0);
            UpgradeRule();
            this.currentLevel++;
            LocalDB.Save(DBKeys.CurrentLevel, currentLevel);
        }
    }

    public int UpgradeRule()
    {
        this.currentLevel = Bingo.Facades.LocalDB.Get<int>(Bingo.DBKeys.CurrentLevel, 1);
        this.targetMergeCount = baseMergeCount + (currentLevel - 1) * increaseNum;
        return targetMergeCount;
    }

    public void ResetLevel()
    {
        Bingo.Facades.LocalDB.Save(Bingo.DBKeys.MergeCount,0);
        Bingo.Facades.LocalDB.Save(Bingo.DBKeys.CurrentLevel,1);
    }

    public int GetANewNumber()
    {
        ++this.callCount;
        this.currentToGive = this.nextToGive;
        this.nextToGive = GetNumber();//UnityEngine.Random.Range(this.MIN_VALUE,this.MAX_VALUE + 1);
        return this.currentToGive;
    }


    /// <summary>
    /// 数字交换
    /// </summary>
    public void ExchangeNumber()
    {
        int num = this.nextToGive;
        this.nextToGive = this.currentToGive;
        this.currentToGive = num ;
    }

    public void SetNewNumber(int a,int b)
    {
        this.currentToGive = a;
        this.nextToGive = b;
    }
    
    public void GetCurrentNumber(int value)
    {
        this.currentToGive = value;
        this.nextToGive = GetNumber();//UnityEngine.Random.Range(this.MIN_VALUE, this.MAX_VALUE + 1);
    }

    
    
    public int GetCurrentNumber()
    {
        return this.currentToGive;
    }

    public int GetNextNumber()
    {
        return this.nextToGive;
    }

    public void discardClicked()
    {
        if (this.nextToGive != this.currentToGive)
            return;
        int num = 500;
        while (this.nextToGive == this.currentToGive)
        {
            this.nextToGive = GetNumber();
            --num;
            if (num < 0)
                break;
        }
    }

    public void StartFromLoad(int readyNum, int nextNum)
    {
        this.currentToGive = readyNum;
        this.nextToGive = nextNum;
    }

    public int GetNumber()
    {
        return  UnityEngine.Random.Range(this.MIN_VALUE, this.MAX_VALUE + 1);
    }
}